package com.hwua.tonyshop.controller.fg;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hwua.tonyshop.criteria.CartInfoCriteria;
import com.hwua.tonyshop.entity.mojo.CartInfoAO;
import com.hwua.tonyshop.entity.mojo.GoodsInfoAO;
import com.hwua.tonyshop.entity.pojo.CartInfo;
import com.hwua.tonyshop.entity.pojo.User;
import com.hwua.tonyshop.service.ICartInfoService;
import com.hwua.tonyshop.serviceImpl.CartInfoService;
import com.hwua.tonyshop.serviceImpl.UserService;
import com.slkj.autocode.entity.ServiceResult;

@Controller
@RequestMapping(value = "/fore/cart")
public class CartController {
	@Resource
	ICartInfoService cartInfoService;
	/**
	 * 跳转到购物车页面
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toCartPage", method = { RequestMethod.GET, RequestMethod.POST })
	public String toCartPage(String token,Model model,HttpServletRequest request,HttpServletResponse response) {
		model.addAttribute("token",token);
		return "foreground/page/shop";
	}
	//获取购物车中的商品信息
	@RequestMapping(value = "/getCartGoodsInfos", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<List<CartInfoAO>> getCartGoodsInfos(String token,Model model,HttpServletRequest request,HttpServletResponse response) {
		User user = (User) request.getSession().getAttribute(token);
		CartInfoCriteria criteria = new CartInfoCriteria();
		criteria.andStateEquals("0").andUserIdEquals(user.getId());
		return cartInfoService.selectByCriteria(criteria);
	}
	//删除购物车中的商品
	@RequestMapping(value = "/deleteCartGoodsInfos", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> deleteCartGoodsInfos(Integer cartId,Model model,HttpServletRequest request,HttpServletResponse response) {
		return cartInfoService.deleteById(cartId);
	}
	//修改购物车中的商品数量
	@RequestMapping(value = "/updateCartGoodsInfos", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> updateCartGoodsInfos(CartInfo cartInfo,Model model,HttpServletRequest request,HttpServletResponse response) {
		return cartInfoService.updateById(cartInfo);
	}
	//添加购物车
	@RequestMapping(value = "/addCartGoodsInfos", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public ServiceResult<Integer> addCartGoodsInfos(CartInfo cartInfo,Model model,HttpServletRequest request,HttpServletResponse response) {
		return cartInfoService.addCartInfo(cartInfo);
	}
}
